unit debugreader;

interface
uses Classes, Windows, ShellAPI, Dialogs, SysUtils, version;

type
  TDebugReader = class(TThread)
  public
    hPipeRead  : THandle;
    EventReady : THandle;
    Output     : string;
    Idling    : boolean;

  protected
    procedure Execute; override;

  end;

implementation

procedure TDebugReader.Execute;
var
  lpBuffer : array [0..256] of char;
  nBytesRead : DWORD;
  _output : string;
begin
  _output := '';
  while true do begin
    FillChar(lpBuffer, sizeof(lpBuffer), 0);
    if (not ReadFile(hPipeRead, lpBuffer, sizeof(lpBuffer),
        nBytesRead, nil) or (nBytesRead = 0)) then begin
      if (GetLastError() = ERROR_BROKEN_PIPE) then
        break // pipe done - normal exit path.
      else
        break; // Something bad happened.
    end;
    _output := _output + string(lpBuffer);
    if pos(GDB_PROMPT, _output) <> 0 then begin
      SetEvent(EventReady);
      Output := _output;
      _output := '';
      Idling := true;
    end;
  end;
end;

end.
